<?php

namespace app\controller\backed;

use app\BaseController;
use app\lib\JwtHander;
use think\facade\Cache;
use app\lib\exception\NotFoundException;
use app\lib\exception\VerifyException;
use think\facade\Config;
use app\model\DingGroup as DingGroupModel;
use app\model\DingUser as DingUserModel;


class Roster extends BaseController
{

    public function getGroupList()
    {
        $dingGroupModel = new DingGroupModel;

        $res = $dingGroupModel->field('id,name,parent_id')->where('id != 1')->order('parent_id')->select();

        $data = getTree($res, 1);

        return $this->successMessage('查询成功',$data);        
    }

    public function getAllRosters()
    {

        $params = request()->param();

        $dingUserModel = new DingUserModel;
        $where = [];
        if(isset($params['name'])){
            $where[] = ['name', 'like','%' . $params['name'] . '%'];
        }
        if(isset($params['group'])){
            $where[] = ['department', 'like','%' . $params['group'] . '%'];
        }

        $res = $dingUserModel->where($where)->select();
        return $this->successMessage('查询成功',$res);           
    }


    public function getRosterInfo()
    {
        $params = request()->param();
        $uid = $params['uid'];
        $res = DingUserModel::find($uid);
        return $this->successMessage('查询成功',$res);
    }

    // 员工列表
    public function getRosterList()
    {
    
        $params = request()->param();

        $dingGroupModel = new DingGroupModel;

        $where = [];
        if(isset($params['name'])){
            $where[] = ['name', 'like','%' . $params['name'] . '%'];
        }
        if(isset($params['group'])){
            $where[] = ['department', 'like','%' . $params['group'] . '%'];
        }

        // 是否离职 0:在职;1:已离职;2:待离职;
        if(isset($params['is_quit'])){
            $where[] = ['is_quit', '=',$params['is_quit']];
        }

        $pageNo = isset($params['pageNo']) ? $params['pageNo'] : 1;
        $pageSize = isset($params['pageSize']) ? $params['pageSize'] : 20;

        $res = DingUserModel::where($where)->order('id', 'asc')->paginate([
            'page' => $pageNo,
            'list_rows' => $pageSize
        ],false);


        foreach ($res as $key => &$item) {
            $item['department'] = $dingGroupModel->find(explode(',', $item['department'])[0])['name'];
        }

        return $this->successMessage('查询成功',$res);

    }

}